<?php 
namespace app\home\logic;
class Tuling{
    /*
    图灵返回code错误码定义
    code - 说明
    0       返回成功
    3**     操作错误
    301     缺少必要参数，参数格式错误
    4**     权限错误
    401     APIkey不存在或者不具有权限
    405     token校验失败
    5**     接口专属错误
    */
    protected $server = "http://www.tuling123.com/v1/";
    protected $action_create = "robot/create";
    protected $action_info = 'robot/info';

    protected $user = 'minei';
    protected $secret = 'Lasa110bM';

    /**
	 * 创建机器人
	 * @param  int      $type  0-娱乐和全能场景，1-智能客服，2-职能FAQ
	 * @return array    图灵服务器返回信息
     *                  {
     *                      "data":{"level":20000,"name":"turing","faq":100000,"scene":"用户接口生成",
     *                              "match":70,"apikey":"apikey"},
     *                      "code":0
     *                  }
	 */
    public function CreateRobot($type){
        switch($type){
            case 0:
            $scene = '用户接口生成';
            break;
            case 1:
            $scene = '智能客服';
            case 2:
            $scene = '职能FAQ';
            default:
            $scene = '用户接口生成';
        }
        $robotData["name"] = 'xiaoweirobot';  //机器人名称
        $robotData["level"] = 20000; //日调用次数
        $robotData['faq'] = 100000;  //知识库总量
        $robotData['mod'] = true;   //知识库加密开关
        $robotData["match"] = 60;    //知识库匹配度
        $robotData["secret"] = $this->secret;   //知识库加密密钥，加密开启时必填
        $robotData["scene"] = $scene;    //使用场景
        
        $data["user"] = $this->user;          //会员用户名
        $data["data"] = $robotData; 
        $data['timestamp'] = time(); //加密必填
        $data['token'] =md5($this->secret.$data['timestamp']);   //MD5加密计算结果（加密必填）

        header("Content-type: text/html; charset=utf-8");
        $req = json_encode($data);
        //向图灵服务器发送创建机器人请求
        $ret = $this->post($this->server.$this->action_create, $req);
        //返回格式
        $jsonRet = json_decode($ret, true);
        return $jsonRet;
    }

    /**
    * 查询机器人
    * @param  string 机器人apikey
    * @return array  机器人信息
        Array
    (
        [data] => Array
            (
                [robotSetting] => Array
                    (
                        [mother] => 
                        [location] => 
                        [nickname] => xiaoweirobot
                        [age] => 
                        [image] => /resources/web/v4/img/personalCen/icon40.png
                        [gender] => M
                        [match] => 60
                        [dislike] => 
                        [like] => 
                        [father] => 
                    )

                [faq] => Array
                    (
                        [total] => 500000
                    )

                [scene] => 用户接口生成
                [ability] => Array
                    (
                        [limit_number] => 1
                        [encyclopedias] => 1
                        [constellation] => 1
                        [cookbook] => 1
                        [news] => 1
                        [number_count] => 1
                        [chat] => 1
                        [date] => 1
                        [express] => 1
                        [fortune] => 1
                        [question_answer] => 1
                        [knowledgebase] => 1
                        [picture] => 1
                        [acrostic] => 1
                        [flight] => 1
                        [idiom_game] => 1
                        [story] => 1
                        [shuangseqiu] => 1
                        [train] => 1
                        [weather] => 1
                        [joke] => 1
                    )

            )

        [code] => 0
    )
    */
    public function Find($apikey){
        $data["apikey"] = $apikey;
        $data['timestamp'] = time(); //加密必填
        $data['token'] =md5($this->secret.$data['timestamp']);   //MD5加密计算结果（加密必填）

        header("Content-type: text/html; charset=utf-8");
        $req = json_encode($data);
        //向图灵服务器发送创建机器人请求
        $ret = $this->post($this->server.$this->action_info, $req);
        //返回格式
        $jsonRet = json_decode($ret, true);
        if($jsonRet['code'] == 0){
            return $jsonRet['data'];
        }else{
            return null;
        }
    }

    private function post($url, $post_data = '', $timeout = 5){//curl
        $header = array(
            'Content-Type: application/json2',
        );
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt ($ch, CURLOPT_POST, 1);
        if($post_data != ''){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }
}